import router from "../router/index";

/**
 * 页面跳转
 * @param target 目标路由
 * @param mode 跳转方式
 * @param open 新开标签页
 */
export function handlePageJump(target, mode = "push", open = false) {
  if (target) {
    if (open) {
      window.open(target);
    } else {
      router[mode](target);
    }
  }
}
// 返回项目路径
export function getNormalPath(p) {
  if (p.length === 0 || !p || p == "undefined") {
    return p;
  }
  let res = p.replace("//", "/");
  if (res[res.length - 1] === "/") {
    return res.slice(0, res.length - 1);
  }
  return res;
}

/**
 * 参数处理
 * @param params 对象
 * @returns {string} ?x=1&y=2...
 */
export function tansParams(params) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    let part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof value[key] !== "undefined") {
            let params = propName + "[" + key + "]";
            let subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return "?" + result.substring(0, result.length - 1);
}

export function checkFull() {
  let isFull = false;
  if (document.fullscreenEnabled || document.msFullscreenEnabled) {
    isFull = window.fullScreen || document.webkitIsFullScreen;
    if (isFull === undefined) {
      isFull = false;
    }
  }
  return isFull;
}
